//
//  YNIMmanager.m
//  YNIMFramework
//
//  Created by 黄旭 on 2021/7/7.
//

#import "YNIMmanager.h"
#import "YNIMTool.h"
#import "YNIMFrameworkMacro.h"
#import "YNRemindManager.h"
#import "YNContactManager.h"
#import "YNMessageContactManager.h"
#import "YNMessagerReceiveHandle.h"
#import "YNSLRoster.h"
#import "YNGroupAndDiscussion.h"
#import "YNXMPPManager.h"
#import "MessageRemind.h"
#import "XMPPDBManager.h"

///这里的遍历操作使用reverseObjectEnumerator逆序的方式，防止在执行delegate的方法时又同时在操作数组的情况导致崩溃
#define allObjToSelector(_selector,_call) \
for (YNXMPPprotocolBridge* obj in self.delegates.reverseObjectEnumerator) {\
    if ([obj.delegate respondsToSelector:@selector(_selector)]) {\
        [obj.delegate _call];\
    }\
}
////////////////////////YNXMPPprotocol协议对象数组里面直接addDelegate对象会造成强引用导致对象不释放
////////////////////////所以需要加一个中间桥梁来设置delegate，数组里面装载这个类//////////////////
@interface YNXMPPprotocolBridge : NSObject
@property(nonatomic,weak)id <YNXMPPprotocol>delegate;
@end
@implementation YNXMPPprotocolBridge
- (instancetype)initWithDelegate:(id<YNXMPPprotocol>)delegate;
{
    self = [super init];
    if (self) {
        self.delegate = delegate;
    }
    return self;
}
@end

@interface YNIMmanager()<YNXMPPprotocol>
{
    YNXMPPManager* ynxmpp;
    NSString* _code;
}
/**HX** 全局记录-拉取下来广播的漫游列表后，记录unreadMsgIds字段里存在的未读广播的id，用以比对标记漫游拉下来的广播是否已读并存库 **/
@property(nonatomic,strong)NSMutableArray<NSString*> *broadcastUnreadMsgIds;
@property(nonatomic,strong)NSMutableDictionary *flagDic;
@property(nonatomic,strong)NSMutableArray<YNXMPPprotocolBridge*> *delegates;
@property(nonatomic,copy,readwrite)NSString *userImId;
@property(nonatomic,copy,readwrite)NSString *userName;
@property(nonatomic,assign,readwrite)int userType;
@end

@implementation YNIMmanager

+ (YNIMmanager *)sharedInstance
{
    static YNIMmanager *groupHandle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        groupHandle = [[YNIMmanager alloc] init];
    });
    return groupHandle;
}

-(void)dealloc{
    ;
}

///-----初始化IM-----
-(void)initXMPPWithServer:(NSString*)server companyCode:(NSString*)code imId:(NSString*)imId userName:(NSString*)userName loginName:(NSString*)loginName dbPath:(NSString*)dbPath userType:(int)userType{
    _code = code;
    self.userImId = imId;
    self.userName = userName;
    self.userType = userType;
    //初始设置数据库管理
    [[XMPPDBManager sharedInstance]creatDatabaseQueueWithPath:dbPath];
    ynxmpp = [[YNXMPPManager alloc]initXMPPWithServer:server companyCode:code imId:imId loginName:loginName delegate:self minimalist:NO];
    [ynxmpp connectWithDomain:kXMPP_domain resource:kResource_Msg_Phone];
    [YNRemindManager initMessageRemindData];
}
-(YNXMPPManager*)getXMPPManager{
    return ynxmpp;
}
//添加事件监听者
-(void)addImDelegate:(id<YNXMPPprotocol>)delegate{
    BOOL have=NO;
    for (YNXMPPprotocolBridge*bridge in self.delegates) {
        if (bridge.delegate == delegate) {
            have=YES;
            break;
        }
    }
    if (!have) {
        [self.delegates addObject:[[YNXMPPprotocolBridge alloc]initWithDelegate:delegate]];
    }
        
}
//移除事件监听者
-(void)removeImDelegate:(id<YNXMPPprotocol>)delegate{
    for (YNXMPPprotocolBridge*bridge in self.delegates) {
        if (bridge.delegate == delegate) {
            bridge.delegate = nil;
            [self.delegates removeObject:bridge];
            break;
        }
    }
}
//添加广播消息id到集合
-(void)addBroadcastUnreadMsgIds:(NSString*)ids{
    [self.broadcastUnreadMsgIds addObject:ids];
}
//设置当前已进入会话页面
-(void)goInChatViewWithUserId:(NSString*)userId{
    if ([YNIMTool isEmptyString:userId]) {
        return;
    }
    [self.flagDic setObject:userId forKey:kFlag_ChatVC];
}
//设置当前已进入广播页面
-(void)goInBroadcastView{
    [self.flagDic setObject:@"BroadcastFlag" forKey:kFlag_BroadcastVC];
}
//设置当前已离开会话页面
-(void)goOutChatView{
    [self.flagDic removeObjectForKey:kFlag_ChatVC];
}
//设置当前已离开广播页面
-(void)goOutBroadcastView{
    [self.flagDic removeObjectForKey:kFlag_BroadcastVC];
}
//获取当前在与谁的会话页面
-(NSString*)getIdCurrChatWithUser{
    return self.flagDic[kFlag_ChatVC];
}
//获取当前是否在广播消息页面
-(NSString*)getIdCurrBroadcastView{
    return self.flagDic[kFlag_BroadcastVC];
}
//清掉广播消息未读id记录数组里存的该条id
-(void)clearBroadcastMsgId:(NSString*)idString{
    if (self.broadcastUnreadMsgIds.count!=0) {
        for (NSString*msgId in self.broadcastUnreadMsgIds) {
            if ([msgId isEqualToString:idString]) {
                [self.broadcastUnreadMsgIds removeObject:msgId];
                break;
            }
        }
    }
}
//清掉广播消息未读id记录数组里的全部数据
-(void)clearBroadcastUnreadIds{
    [self.broadcastUnreadMsgIds removeAllObjects];
}
//判断未读广播缓存里是否有这个广播
-(BOOL)broadcastUnreadsHaveId:(NSString*)packetId{
    for (NSString*broadId in self.broadcastUnreadMsgIds) {
        if ([broadId isEqualToString:packetId]) {
            return NO;
        }
    }
    return YES;
}
/**
 获取漫游消息记录
 */
-(void)getRoamMessageRecordWithChatType:(ReceiptMessageType)messageType chatId:(NSString*)chatId msgCount:(NSInteger)msgCount sendTime:(NSString*)sendTime isMessageNewListDat:(BOOL)isMessageNewListDat{
    [[ynxmpp getRoster]getRoamMessageRecordWithChatType:messageType chatId:chatId msgCount:msgCount sendTime:sendTime isMessageNewListDat:isMessageNewListDat];
}
/**
 漫游删除会话列表的会话消息
 */
-(void)deleteRoamMessageListWithChatType:(ReceiptMessageType)messageType chatId:(NSString*)chatId{
    [[ynxmpp getRoster]deleteRoamMessageListWithChatType:messageType chatId:chatId];
}
//获取Steam连接状态
- (XMPPConnectType)getXMPPConnectStatus{
    return [ynxmpp getXMPPConnectStatus];
}
///判断是否需要重新登录
- (BOOL)isNeedLoginAgain{
    return [ynxmpp isNeedLoginAgain];
}
//退出登录
- (void)logout{
    [ynxmpp logout];
}
/**
 * 查找用户或获取某个用户的详细信息
 **/
- (void)searchDetailedInfomationOfAUser:(NSString *)userId{
    [[ynxmpp getRoster]searchDetailedInfomationOfAUser:userId];
}
///刷新xmpp连接，如果断开则执行重新登录
-(void)refreshXmppConnect{
    [ynxmpp refreshXmppConnect];
}
/**
 撤回消息，发送IQ消息
 */
-(void)updateWithdrawMessageListWithChatType:(ReceiptMessageType)messageType chatId:(NSString*)chatId packetId:(NSString *)packetId sendTime:(NSString *)sendTime{
    [[ynxmpp getRoster]updateWithdrawMessageListWithChatType:messageType chatId:chatId packetId:packetId sendTime:sendTime];
}
/*
 *手动获取组织机构和群讨论组等信息
 */
- (void)manualFetchUserData{
    [[ynxmpp getRoster]manualFetchUserData];
}
/// 发送文本消息
- (void)sendTextMessageToUserId:(NSString *)userId text:(NSString*)text aitArray:(NSString *)aitArray isTranspond:(BOOL)isTranspond groupName:(NSString*)groupName{
    [[ynxmpp getMessageHandle] sendTextMessageToUserId:userId text:text aitArray:aitArray isTranspond:isTranspond groupName:groupName];
}
/// 事先组装好message情况下发送消息
-(void)sendMessage:(YNMessage *)message{
    [[ynxmpp getMessageHandle] sendMessage:message];
}
/// 发送普通消息回执
-(void)sendReceiptMessageToUserId:(NSString*)userId type:(ReceiptMessageType)messageType{
    [[ynxmpp getMessageHandle] sendReceiptMessageToUserId:userId type:messageType];
}
/**HX** 发送广播消息 **/
-(void)sendBroadcastMessage:(NSArray<NSString*>*)reciveUserIds body:(NSString*)body{
    [[ynxmpp getMessageHandle] sendBroadcastMessage:reciveUserIds body:body];
}
/// 重发消息
- (void)reSendMessage:(YNMessage *)message uploadBlock:(uploadChatFileBlock)block{
    [[ynxmpp getMessageHandle] reSendMessage:message uploadBlock:block];
}
/// 发送图片
- (void)sendPhotoMessage:(YNMessage *)message pic:(PictureModel *)pic uploadBlock:(uploadChatFileBlock)block{
    [[ynxmpp getMessageHandle] sendPhotoMessage:message pic:pic uploadBlock:block];
}
///发送语音
- (void)sendVoiceMessage:(YNMessage *)message uploadBlock:(uploadChatFileBlock)block{
    [[ynxmpp getMessageHandle] sendVoiceMessage:message uploadBlock:block];
}
///发送文件
- (void)sendFileMessage:(YNMessage *)message uploadBlock:(uploadChatFileBlock)block{
    [[ynxmpp getMessageHandle] sendFileMessage:message uploadBlock:block];
}
///发送文件消息回执
- (void)sendFileReturnMessage:(YNMessage *)message{
    [[ynxmpp getMessageHandle] sendFileReturnMessage:message];
}
/**
 * 概述: 创建讨论组
 */
- (void)creatDiscussionWithUsers:(NSArray *)array{
    [[ynxmpp getGroupHandle]creatDiscussionWithUsers:array];
}
/**
 * 概述: 创建群
 */
- (void)creatGroupWithUsers:(NSArray *)array GroupName:(NSString *)groupName NaturalName:(NSString *)naturalName subject:(NSString *)subject Desc:(NSString *)desc{
    [[ynxmpp getGroupHandle]creatGroupWithUsers:array GroupName:groupName NaturalName:naturalName subject:subject Desc:desc];
}
/**
 * 概述: 群和讨论组添加成员
 */
- (void)addUserWithGroupName:(NSString *)groupName Users:(NSArray *)array{
    [[ynxmpp getGroupHandle]addUserWithGroupName:groupName Users:array];
}
/**
 * 概述: 退出群和讨论组
 */
- (void)leaveGourpWithGroupName:(NSString *)groupName{
    [[ynxmpp getGroupHandle]leaveGourpWithGroupName:groupName];
}
/**
 * 概述: 获取群信息及群成员
 * @pram groupName 群ID
 */
- (void)getGroupInfoWithGoupName:(NSString *)groupName{
    [[ynxmpp getGroupHandle]getGroupInfoWithGoupName:groupName];
}
/**
 * 概述: 获取群共享文件
 */
- (void)getFileOfGroupShareWithGoupName:(NSString *)groupName{
    [[ynxmpp getGroupHandle]getFileOfGroupShareWithGoupName:groupName];
}
/**
 * 概述: 获取讨论组信息及讨论组成员
 */
- (void)getDiscussionInfoWithDiscussionName:(NSString *)DiscussionName{
    [[ynxmpp getGroupHandle]getDiscussionInfoWithDiscussionName:DiscussionName];
}
/**
 * 概述: 获取项目组信息及项目组成员
 */
- (void)getProjectTeamInfoWithTeamName:(NSString *)ProjectTeamName{
    [[ynxmpp getGroupHandle]getProjectTeamInfoWithTeamName:ProjectTeamName];
}
/**
 * 概述: 获取一批用户的详细信息
 */
- (void)getUserListWithUserNoList:(NSArray *)memberArray{
    [[ynxmpp getGroupHandle]getUserListWithUserNoList:memberArray];
}
/**
 * 概述: 修改群和讨论组公告
 */
- (void)modifyGroupSubject:(NSString *)subjuct GroupName:(NSString *)groupName{
    [[ynxmpp getGroupHandle]modifyGroupSubject:subjuct GroupName:groupName];
}
///更新消息提醒设置
- (BOOL)updateMessageRemindSetting:(RemindSet *)remindSet{
    BOOL result = [YNRemindManager updateMessageRemindSetting:remindSet];
    if (result) {
        [[ynxmpp getMessageHandle]updateRemindSetting];
    }
    return result;
}

#pragma mark YNXMPPprotocol
-(void)xmppStreamDidConnect{
    NSString * mac = [YNIMTool identifierForVendor1];
    float  IOSVersion =[[[UIDevice currentDevice] systemVersion] floatValue];
    NSString * mobilesystem =[NSString stringWithFormat:@"ios%f",IOSVersion];
    if(mobilesystem.length>5)
    {
        NSRange range = [mobilesystem rangeOfString:@"."];
        mobilesystem =[mobilesystem substringToIndex:range.location+2];
    }
    //现在不区分，统一用这个
    NSString*passwdKey=@"verificationType";
    NSDictionary *pwdDicInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"1", passwdKey, @"100", @"cversion",_code,@"code",@"3",@"type",@"",@"localip",@"",@"reqip",mac,@"mac",mobilesystem,@"mobilesystem",@"1",@"appflag", nil];
    NSString *pwdJson = [[YNIMTool jsonStringWithObject:pwdDicInfo] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *pwdEncrypt = [YNIMTool encryptUseDES:pwdJson key:kXMPP_DESKEY iv:kXMPP_DESKEY];
    [ynxmpp xmppAuthenticatePassword:pwdEncrypt];
    allObjToSelector(xmppStreamDidConnect, xmppStreamDidConnect)
}
-(void)xmppStreamDidAuthenticate{
    [ynxmpp goOnline];
    [ynxmpp getLoginUserInfor];
    allObjToSelector(xmppStreamDidAuthenticate, xmppStreamDidAuthenticate)
}
-(void)didNotAuthenticate{
    allObjToSelector(didNotAuthenticate, didNotAuthenticate)
}
-(void)xmppStreamConnectDidTimeout{
    allObjToSelector(xmppStreamConnectDidTimeout, xmppStreamConnectDidTimeout)
}
-(void)sameTimeLogin{
    allObjToSelector(sameTimeLogin, sameTimeLogin)
}
-(void)xmppStreamDidDisconnect:(NSInteger)code{
    switch (code)
    {
        case 61:
            //服务器连接异常
            break;
        case 64:
            //服务器连接异常
            break;
        case 60:
            //服务器连接异常
            break;
        case 49:
            //服务器连接异常
            break;
        case 4:
            //服务器连接异常
            break;
        default:
            //服务器连接异常
            break;
    }
    allObjToSelector(xmppStreamDidDisconnect:, xmppStreamDidDisconnect:code)
}
-(void)getOrgAndUserDataSaveFinished:(NSDictionary *)dic{
    allObjToSelector(getOrgAndUserDataSaveFinished:, getOrgAndUserDataSaveFinished:dic)
}
-(void)getOrgAndUserDataSaveFail{
    allObjToSelector(getOrgAndUserDataSaveFail, getOrgAndUserDataSaveFail)
}
-(void)getLoginUserInitInforSucceed:(NSDictionary *)dic{
    allObjToSelector(getLoginUserInitInforSucceed:, getLoginUserInitInforSucceed:dic)
    [ynxmpp fetchRosterData];
}
-(void)groupInforSaveFinished{
    allObjToSelector(groupInforSaveFinished, groupInforSaveFinished)
}
-(void)discussionSaveFinished{
    allObjToSelector(discussionSaveFinished, discussionSaveFinished)
}
-(void)projectTeamSaveFinished{
    allObjToSelector(projectTeamSaveFinished, projectTeamSaveFinished)
}
-(void)deleteRoamMessageListSuccess:(bool)result{
    allObjToSelector(deleteRoamMessageListSuccess:, deleteRoamMessageListSuccess:result)
}
-(void)messengerChatWithDrawUpdateMsgId:(NSString*)msgId success:(BOOL)success{
    allObjToSelector(messengerChatWithDrawUpdateMsgId:success:, messengerChatWithDrawUpdateMsgId:msgId success:success)
}
-(void)loginLoadDataFinish{
    allObjToSelector(loginLoadDataFinish, loginLoadDataFinish)
}
//更新消息会话页面
-(void)updataRecentChatInterface{
    allObjToSelector(updataRecentChatInterface, updataRecentChatInterface)
}
//搜索默认的结果回调
-(void)ynslRosterDidSearchInfo:(NSDictionary*)dic{
    allObjToSelector(ynslRosterDidSearchInfo:, ynslRosterDidSearchInfo:(NSDictionary*)dic)
}
//拉取一次漫游消息成功
-(void)pullTheRoamingMessageFinished{
    allObjToSelector(pullTheRoamingMessageFinished, pullTheRoamingMessageFinished)
}

-(void)receiveChatMessage:(YNMessage*)message{
    allObjToSelector(receiveChatMessage:, receiveChatMessage:message)
}
//接受到回执消息
-(void)receiveMessageReceipt:(YNMessage*)message{
    allObjToSelector(receiveMessageReceipt:, receiveMessageReceipt:message)
}
//创建讨论组请求成功
- (void)createGroupWithRequestFinished:(NSString *)groupName naturalName:(NSString *)naturalName{
    allObjToSelector(createGroupWithRequestFinished:naturalName:,createGroupWithRequestFinished:(NSString *)groupName naturalName:(NSString *)naturalName)
}
//创建讨论组请求失败
- (void)createGroupWithRequestFailed:(NSString *)error{
    allObjToSelector(createGroupWithRequestFailed:, createGroupWithRequestFailed:(NSString *)error)
}
- (void)reciveAddGroupUserInfo:(BOOL)isSuccss{
    allObjToSelector(reciveAddGroupUserInfo:, reciveAddGroupUserInfo:(BOOL)isSuccss)
}
- (void)reciveLeaveGroupInfo:(BOOL)isSuccss{
    allObjToSelector(reciveLeaveGroupInfo:, reciveLeaveGroupInfo:(BOOL)isSuccss)
}
- (void)reciveModifyGroupSubject:(BOOL)isSuccss resultDictionary:(NSDictionary *)dic{
    allObjToSelector(reciveModifyGroupSubject:resultDictionary:, reciveModifyGroupSubject:(BOOL)isSuccss resultDictionary:(NSDictionary *)dic)
}
- (void)reciveGroupInfo:(NSDictionary *)dic{
    allObjToSelector(reciveGroupInfo:, reciveGroupInfo:(NSDictionary *)dic)
}
- (void)reciveDiscussionInfo:(NSDictionary *)dic{
    allObjToSelector(reciveDiscussionInfo:, reciveDiscussionInfo:(NSDictionary *)dic)
}
- (void)reciveProjectTeamInfo:(NSDictionary *)dic{
    allObjToSelector(reciveProjectTeamInfo:, reciveProjectTeamInfo:(NSDictionary *)dic)
}
- (void)reciveFileInfoOfGroupShare:(NSDictionary *)dic{
    allObjToSelector(reciveFileInfoOfGroupShare:, reciveFileInfoOfGroupShare:(NSDictionary *)dic)
}
//成功删除群共享文件
- (void)didFinishedDeleteShareGroupFile:(NSString *)fileId{
    allObjToSelector(didFinishedDeleteShareGroupFile:, didFinishedDeleteShareGroupFile:(NSString *)fileId)
}
//删除群共享文件
- (void)failedDeleteShareGroupFile:(NSString *)fileId{
    allObjToSelector(failedDeleteShareGroupFile:, failedDeleteShareGroupFile:(NSString *)fileId)
}
/**
 * 概述: 获取一批用户的详细信息
 */
- (void)reciveUserList:(BOOL)isSuccss userListDictionary:(NSArray *)array{
    allObjToSelector(reciveUserList:userListDictionary:, reciveUserList:(BOOL)isSuccss userListDictionary:(NSArray *)array)
}
//讨论组名字被修改的服务器通知
- (void)reciveManyChatGroupNameWithModify:(NSString *)subject{
    allObjToSelector(reciveManyChatGroupNameWithModify:, reciveManyChatGroupNameWithModify:(NSString *)subject)
}
//创建讨论组返回的presence(状态信息)
- (void)reciveGroupCreatWithGroupNo:(NSString *)groupNo userNo:(NSString *)userNo  affiliation:(NSString *)affiliation role:(NSString *)role{
    allObjToSelector(reciveGroupCreatWithGroupNo:userNo:affiliation:role:,reciveGroupCreatWithGroupNo:(NSString *)groupNo userNo:(NSString *)userNo  affiliation:(NSString *)affiliation role:(NSString *)role)
}
//退出讨论组返回的presence
- (void)reciveLeaveGroupWithGoupNo:(NSString *)groupNo userNo:(NSString *)userNo affiliation:(NSString *)affiliation role:(NSString *)role{
    allObjToSelector(reciveLeaveGroupWithGoupNo:userNo:affiliation:role:,reciveLeaveGroupWithGoupNo:(NSString *)groupNo userNo:(NSString *)userNo affiliation:(NSString *)affiliation role:(NSString *)role)
}
//转发消息通知
-(void)transpondMessageNotification:(YNMessage *)message{
    allObjToSelector(transpondMessageNotification:, transpondMessageNotification:(YNMessage *)message)
}
- (void)receiveBroadcastUpdataInterface:(YNMessage *)message{
    allObjToSelector(receiveBroadcastUpdataInterface:, receiveBroadcastUpdataInterface:(YNMessage *)message)
}
-(void)updateTheBroadcastReaedForId:(NSString *)idString{
    allObjToSelector(updateTheBroadcastReaedForId:, updateTheBroadcastReaedForId:(NSString *)idString)
}
-(void)modifyDiscussionGroupName:(NSString *)name{
    allObjToSelector(modifyDiscussionGroupName:, modifyDiscussionGroupName:name)
}
-(void)modifyGroupName:(NSString *)name{
    allObjToSelector(modifyGroupName:, modifyGroupName:name)
}
-(void)leaveDiscussionGroupName:(NSString *)name{
    allObjToSelector(leaveDiscussionGroupName:, leaveDiscussionGroupName:name)
}
-(void)leaveGroupName:(NSString *)name{
    allObjToSelector(leaveGroupName:, leaveGroupName:name)
}
-(void)leaveProjectTeamName:(NSString *)name{
    allObjToSelector(leaveProjectTeamName:, leaveProjectTeamName:name)
}
-(void)exitDiscussionGroupName:(NSString *)name{
    allObjToSelector(exitDiscussionGroupName:, exitDiscussionGroupName:name)
}
-(void)xmppIMuserLogout:(YNUser *)user{
    allObjToSelector(xmppIMuserLogout:, xmppIMuserLogout:user)
}
-(void)xmppIMuserLogin:(YNUser *)user{
    allObjToSelector(xmppIMuserLogin:, xmppIMuserLogin:user)
}
-(void)exitGroupName:(NSString *)name{
    allObjToSelector(exitGroupName:, exitGroupName:name)
}
-(void)exitProjectTeamGroupName:(NSString *)name{
    allObjToSelector(exitProjectTeamGroupName:, exitProjectTeamGroupName:name)
}
-(void)updateGroupName:(NSString *)name{
    allObjToSelector(updateGroupName:, updateGroupName:name)
}
-(void)updateDiscussionGroupName:(NSString *)name{
    allObjToSelector(updateDiscussionGroupName:, updateDiscussionGroupName:name)
}
-(void)updateProjectTeamGroupName:(NSString *)name{
    allObjToSelector(updateProjectTeamGroupName:, updateProjectTeamGroupName:name)
}
-(void)receiveChatMesWithRecentChatId:(NSString *)chatId userNo:(NSString *)userNo{
    allObjToSelector(receiveChatMesWithRecentChatId:userNo:, receiveChatMesWithRecentChatId:chatId userNo:userNo)
}

#pragma mark YNXMPPprotocol end

-(NSMutableArray<NSString *> *)broadcastUnreadMsgIds{
    if (!_broadcastUnreadMsgIds) {
        _broadcastUnreadMsgIds=[NSMutableArray array];
    }
    return _broadcastUnreadMsgIds;
}
-(NSMutableDictionary *)flagDic{
    if (!_flagDic) {
        _flagDic=[NSMutableDictionary dictionary];
    }
    return _flagDic;
}
-(NSMutableArray<YNXMPPprotocolBridge*>*)delegates{
    if (!_delegates) {
        _delegates = [NSMutableArray array];
    }
    return _delegates;
}

#pragma mark////////////////////////////////类静态方法调用///////////////////////////////////////////////
///查询消息提醒设置信息
+ (RemindSet *)queryMessageRemindSetting{
    return [YNRemindManager queryMessageRemindSetting];
}
///通过群/讨论组ID查询消息提醒信息 0 不提醒 1 提醒
+ (NSInteger)queryMessageRemindWithGroupID:(NSString *)groupID{
    MessageRemind *messRemind = [YNRemindManager queryMessageRemindWithGroupID:groupID];
    return messRemind.isRemind;
}
///更新群/讨论组消息提醒信息方式type  0 不提醒 1 提醒
+ (BOOL)updateMessageRemindWithGroupID:(NSString *)groupId type:(NSInteger)type{
    MessageRemind *messRemind = [YNRemindManager queryMessageRemindWithGroupID:groupId];
    messRemind.isRemind = type;
    return [YNRemindManager updateMessageRemind:messRemind];
}
///视频会议间收到聊天消息解析
+(void)meetingReceiveMessage:(YNMessage *)message messageControType:(YNBubbleMessageType)controType{
    [YNMessagerReceiveHandle meetingReceiveMessage:message messageControType:controType];
}
/**
 *  解析富文本等消息返回一个数组
 *  或者 是 [字符串|图片|表情 ... , 表情个数, 是否图片文本混排]
 */
+ (NSMutableArray*)parserMessageStr:(NSString *)message messageBody:(NSString *)messageBody{
    return [YNMessagerReceiveHandle parserMessageStr:message messageBody:messageBody];
}
/**
 判断群，讨论组，项目组，两人会话
 
 @param userNo
 @return 0：二人会话 1:群 2：讨论组 3：项目组
 */
+(CurrenChatObjType)getCurrentMessageChatTypemanyPeopleChatWith:(NSString *)userNo{
    return [YNMessagerReceiveHandle getCurrentMessageChatTypemanyPeopleChatWith:userNo];
}
//删除数据库中所有组织机构，用户，组织机构关系树
+ (BOOL)deleteAllUserAndOrgTree{
    return [YNContactManager deleteAllUserAndOrgTree];
}
//2016-9-9jc添加，逻辑删除数据库中所有组织机构，用户，组织机构关系树
+(BOOL)logicDeleteAllUserAndOrgTree{
    return [YNContactManager logicDeleteAllUserAndOrgTree];
}
/**
 获取组织机构数据库数据的条数，也相当于判断其是否有数据
 
 @return 数据数
 */
+ (NSUInteger)getOrgTreeDataCountInDatabase{
    return [YNContactManager getOrgTreeDataCountInDatabase];
}

//登陆成功后保存组织机构信息和登录用户信息
+ (BOOL)saveContactOrgWithOrgDictionary:(NSDictionary *)dic{
    return [YNContactManager saveContactOrgWithOrgDictionary:(NSDictionary *)dic];
}

//保存群/讨论组信息
+ (BOOL)saveContactGroupWithGroupType:(int)groupType orgDictionary:(NSDictionary *)dic{
    return [YNContactManager saveContactGroupWithGroupType:(int)groupType orgDictionary:(NSDictionary *)dic];
}

/**
 @brief 根据parentOrgNo节点查询下面的用户或部门
 
 @param parentOrgNo 组织机构的上级ID，等于0时为根节点
 @param orgType 查询组织机构类型（1：查询所有，2：查询联系人班级，3：查询组织机构下面信息）
 @param allStudent 当上一个参数为contactOrgType时用此参数 yes：查询所有班级数据 NO:查询2-行政班、3-教学班
 @param needAllCount 是否需要计算总人数
 @param needOnlinCount 是否需要计算在线人数
 @param needAllUsers 是否需要返回组织机构下的所有user对象
 @param parentOrgNoS 多个父节点一并查询的父ID字符串
 @param start 分页查询需要的开始行节点
 @param lench 分页查询需要的一次获取数据的条数
 @param countBlock 在分线程里完成总共和在线人数统计的回调
 @return 包含用户或部门的字典
 */
+ (NSDictionary *)queryContactOrgByParentOrgNo:(NSString *)parentOrgNo orgType:(OrgType)orgType getAllStudentDataS:(BOOL)allStudent needCalcuAllCount:(BOOL)needAllCount needCalcuOnlineCount:(BOOL)needOnlinCount needAllUsers:(BOOL)needAllUsers orgNoStringS:(NSString*)parentOrgNoS limitStart:(NSUInteger)start limitLength:(NSUInteger)length completeCount:(void(^)(void))countBlock{
    return [YNContactManager queryContactOrgByParentOrgNo:(NSString *)parentOrgNo orgType:(OrgType)orgType getAllStudentDataS:(BOOL)allStudent needCalcuAllCount:(BOOL)needAllCount needCalcuOnlineCount:(BOOL)needOnlinCount needAllUsers:(BOOL)needAllUsers orgNoStringS:(NSString*)parentOrgNoS limitStart:(NSUInteger)start limitLength:(NSUInteger)length completeCount:countBlock];
}

/**
 @brief 根据parentOrgNo节点查询下面的用户或部门
 老师-联系人-学生界面   查询实习活动数据
 
 @param parentOrgNo 组织机构的上级ID，等于0时为根节点
 @param orgType 查询组织机构类型（1：查询所有，2：查询联系人班级，3：查询组织机构下面信息）
 @param needAllCount 是否需要计算总人数
 @param needOnlinCount 是否需要计算在线人数
 @param needAllUsers 是否需要返回组织机构下的所有user对象
 @param countBlock 在分线程里完成总共和在线人数统计的回调
 @return 包含用户或部门的字典
 */
+ (NSDictionary *)queryContactOrgInternshipByParentOrgNo:(NSString *)parentOrgNo orgType:(OrgType)orgType needCalcuAllCount:(BOOL)needAllCount needCalcuOnlineCount:(BOOL)needOnlinCount needAllUsers:(BOOL)needAllUsers completeCount:(void(^)(void))countBlock{
    return [YNContactManager queryContactOrgInternshipByParentOrgNo:(NSString *)parentOrgNo orgType:(OrgType)orgType needCalcuAllCount:(BOOL)needAllCount needCalcuOnlineCount:(BOOL)needOnlinCount needAllUsers:(BOOL)needAllUsers completeCount:countBlock];
}

/**
 计算该组织机构里的总人数数量
 
 @param orgType 类型
 @param parentOrgNo 组织id
 @param allStudent 当 orgType为contactOrgType时有效
 @return 数量
 */
+(NSInteger)calculateOrgTotalUserNumberWithorgType:(OrgType)orgType getAllStudent:(BOOL)allStudent OrgNo:(NSString *)parentOrgNo{
    return [YNContactManager calculateOrgTotalUserNumberWithorgType:(OrgType)orgType getAllStudent:(BOOL)allStudent OrgNo:(NSString *)parentOrgNo];
}

/**
 *  @brief 获取所有学生信息用户
 *
 *  @return 所有学生信息用户
 */
+ (NSArray *)queryAllStudentUser{
    return [YNContactManager queryAllStudentUser];
}

//根据gruoupName查询用户
+ (NSArray *)queryByGruoupName:(NSString *)groupName isAdmin:(BOOL)isAdmin onlyGetTeachers:(BOOL)onlyTeacher{
    return [YNContactManager queryUserByGroupName:groupName isAdmin:isAdmin onlyGetTeachers:onlyTeacher];
}

/**
 *  @brief 获取所有老师信息用户
 *
 *  @return 所有老师信息用户
 */
+ (NSArray *)queryAllTeacherUser{
    return [YNContactManager queryAllTeacherUser];
}

/**
 *  @brief  离线时清除所有用户在线状态
 *
 *  @return YES 执行成功 NO 失败
 */
+ (BOOL)clearUserOnlineStatusAtTheTimeOfOffline{
    return [YNContactManager clearUserOnlineStatusAtTheTimeOfOffline];
}

//更新用户状态
+ (BOOL)updateUserStatus:(YNUser *)user updatePcStatus:(BOOL)pcStatus updatePhoneStatus:(BOOL)phoneStatus{
    return [YNContactManager updateUserStatus:(YNUser *)user updatePcStatus:(BOOL)pcStatus updatePhoneStatus:(BOOL)phoneStatus];
}

//更新用户头像url
+ (BOOL)updateUserheadUrl:(YNUser *)user{
    return [YNContactManager updateUserheadUrl:(YNUser *)user];
}

/**
 *  @brief 更新一条用户头像url
 *
 *  @param user 用户对象
 */
+ (BOOL)updateUserheadUrl:(YNUser *)user inDatabase:(FMDatabase *)db{
    return [YNContactManager updateUserheadUrl:(YNUser *)user inDatabase:(FMDatabase *)db];
}

/**
 *  保存所有用户在线信息
 *
 *  @param userStatusArray
 */
+ (BOOL)updateAllUserStatus:(NSDictionary *)statusDic{
    return [YNContactManager updateAllUserStatus:(NSDictionary *)statusDic];
}

/**
 *  @brief  搜索联系人
 *
 *  @param  keywords 搜索关键字
 *  @param  onlyTeacher 是否只搜教师相关数据
 *  @param  orgType 搜索的组织机构类型
 *  @return 搜索结果
 */
+ (NSDictionary *)searchContactResultByKeywords:(NSString *)keywords canSearchMe:(BOOL)searchMe  onlyGetTeacherData:(BOOL)onlyTeacher orgType:(OrgType)orgType{
    return [YNContactManager searchContactResultByKeywords:(NSString *)keywords canSearchMe:(BOOL)searchMe  onlyGetTeacherData:(BOOL)onlyTeacher orgType:(OrgType)orgType];
}

//查询所有群/讨论组
+ (NSArray *)queryContactGroup:(int)groupType onlyGetTeachers:(BOOL)onlyTeacher{
    return [YNContactManager queryContactGroup:(int)groupType onlyGetTeachers:(BOOL)onlyTeacher];
}

//查询讨论组创建者
+ (NSArray<NSString*> *)queryCreatorOfDiscussionGroup{
    return [YNContactManager queryCreatorOfDiscussionGroup];
}

//更新用户信息
+ (BOOL)updateUserInformation:(YNUser *)user{
    return [YNContactManager updateUserInformation:(YNUser *)user];
}

//更新群或讨论组信息
+ (BOOL)updateContactGroup:(YNContactGroup *)group{
    return [YNContactManager updateContactGroup:(YNContactGroup *)group];
}

//获取当前用户
+ (YNUser *)queryCurrentUser{
    return [YNContactManager queryCurrentUser];
}

//根据用户imID查询用户
+ (YNUser *)queryUserWithUserNo:(NSString *)userNo{
    return [YNContactManager queryUserWithUserNo:(NSString *)userNo];
}

//根据用户ID查询用户
+ (YNUser *)queryUserWithUserId:(NSString *)userId{
    return [YNContactManager queryUserWithUserId:(NSString *)userId];
}

//组织机构ID判断组织机构是否存在
+ (BOOL)whetherTheOrganTreeExists:(NSString *)organNo{
    return [YNContactManager whetherTheOrganTreeExists:(NSString *)organNo];
}

// 判断群组/讨论组是否存在
+(BOOL)isContactGroupExistIn:(NSString *)groupName groupType:(int)groupType{
    return [YNContactManager isContactGroupExistIn:(NSString *)groupName groupType:(int)groupType];
}

//查询群管理员
+ (NSArray *)queryContactGroupAdministratorByGroupName:(NSString *)groupName{
    return [YNContactManager queryContactGroupAdministratorByGroupName:(NSString *)groupName];
}

//查询用户所在组织机构
+ (YNOrgTree *)queryOrgByUserNo:(NSString *)userNo{
    return [YNContactManager queryOrgByUserNo:(NSString *)userNo];
}

//遍历所有上级组织机构
+ (NSMutableArray *)queryAllParentOrgByParentOrgNo:(NSString *)parentOrgNo{
    return [YNContactManager queryAllParentOrgByParentOrgNo:(NSString *)parentOrgNo];
}

/**
 *  @brief  根据群/讨论组ID获取群/讨论组信息
 *
 *  @param groupNo   群/讨论组ID
 *  @param groupType
 *
 *  @return
 */
+ (YNContactGroup *)queryGroupByGroupNo:(NSString *)groupNo groupType:(int)groupType{
    return [YNContactManager queryGroupByGroupNo:(NSString *)groupNo groupType:(int)groupType];
}

/**
 *  @brief  增量保存群/讨论组所有信息
 *
 *  @param dictionary
 *  @param groupType
 */
+ (BOOL)saveIncrementalGroupDataWithDictionary:(NSDictionary *)dictionary groupType:(int)groupType{
    return [YNContactManager saveIncrementalGroupDataWithDictionary:(NSDictionary *)dictionary groupType:(int)groupType];
}
/**
 *  @brief 创建讨论组
 *
 *  @param group       讨论组信息
 *  @param memberArray 讨论组成员
 */
+ (BOOL)createDiscussionGroup:(YNContactGroup *)group memberArray:(NSArray *)memberArray{
    return [YNContactManager createDiscussionGroup:(YNContactGroup *)group memberArray:(NSArray *)memberArray];
}

/**
 *  @brief 添加群/讨论组 成员
 *
 *  @param group       讨论组信息
 *  @param memberArray 添加的讨论组成员
 */
+ (BOOL)addGroupMemberWithGroup:(YNContactGroup *)group addMemberArray:(NSArray *)memberArray{
    return [YNContactManager addGroupMemberWithGroup:(YNContactGroup *)group addMemberArray:(NSArray *)memberArray];
}

/**
 *  @brief 解散群/讨论组
 *
 *  @param group 群/讨论组信息
 *
 *  @return YES 执行成功 NO 失败
 */
+ (BOOL)dissolveGroupByGroup:(YNContactGroup *)group{
    return [YNContactManager dissolveGroupByGroup:(YNContactGroup *)group];
}
/**
 *  @brief 踢出或离开讨论组
 *
 *  @param user  讨论组成员信息
 *  @param group 讨论组信息
 */
+ (BOOL)leaveGroupWithUser:(YNUser *)user group:(YNContactGroup *)group{
    return [YNContactManager leaveGroupWithUser:(YNUser *)user group:(YNContactGroup *)group];
}


//清除聊天记录
+ (BOOL)deleteChatRecord{
    return [YNContactManager deleteChatRecord];
}


/**
 *  @brief  判断群/讨论组成员是否存在
 *
 *  @param userNo    成员ID
 *  @param groupName 群/讨论组 ID
 *  @param groupType
 *
 *  @return YES ： 存在  NO ： 不存在
 */
+ (BOOL)whetherGroupOfMembers:(NSString *)userNo withGroupNo:(NSString *)groupName groupType:(int)groupType{
    return [YNContactManager whetherGroupOfMembers:(NSString *)userNo withGroupNo:(NSString *)groupName groupType:(int)groupType];
}

/**
 *  @brief 根据群/讨论组Id获取下面的用户
 *
 *  @param groupName 群/讨论组Id
 *  @param groupType 0:群 1:讨论组
 *  @param isAdmin YES 是管理员 NO不是
 *  @param onlyTeacher 只获取老师的数据
 *  @return 一个群/讨论组的成员数组
 */
+ (NSArray *)queryUserByGroupName:(NSString *)groupName isAdmin:(BOOL)isAdmin onlyGetTeachers:(BOOL)onlyTeacher{
    return [YNContactManager queryUserByGroupName:(NSString *)groupName isAdmin:(BOOL)isAdmin onlyGetTeachers:(BOOL)onlyTeacher];
}
/**
 * @brief 保存一条会话 (插入和更新)
 *
 * @param recentChat 会话信息
 */
+ (BOOL)saveRecentChatWithMessage:(YNRecentChatObejct *)recentChat isFromRoamingSource:(BOOL)isRoaming{
    return [YNContactManager saveRecentChatWithMessage:(YNRecentChatObejct *)recentChat isFromRoamingSource:(BOOL)isRoaming];
}

/**
 2019-01 修改 查询所有最近会话消息列表数据
 
 @param db <#db description#>
 @param needScreenTop 是否需要赛选处理置顶消息
 @return 集合
 */
+ (NSArray<YNRecentChatObejct*> *)queryAllRecentChatWithNeedScreenTopChat:(BOOL)needScreenTop{
    return [YNContactManager queryAllRecentChatWithNeedScreenTopChat:(BOOL)needScreenTop];
}

/**
 * @brief 删除会话
 * @param UserNo 要删除会话消息的用户的UserNo, 如果传进来为
 *               nil，清空数据库里所有会话消息
 */
+ (BOOL)deleteRecentChatWithRecentChatObject:(NSString *)UserNo{
    return [YNContactManager deleteRecentChatWithRecentChatObject:(NSString *)UserNo];
}

/**
 *  功能: 根据ID查询一条最近会话
 *  return:一个最近会话对象
 */
+ (YNRecentChatObejct *)queryRecentChatWithFriendId:(NSString *)friendId{
    return [YNContactManager queryRecentChatWithFriendId:(NSString *)friendId];
}

#pragma mark - TwoPeopleChat Method

/**
 * @brief 保存一条消息到历史消息表
 *
 * @param twoPeopleChat 会话消息
 */
+ (BOOL)saveTwoPeopleChatWithMessage:(YNTwoPeopleChat *)twoPeopleChat{
    return [YNContactManager saveTwoPeopleChatWithMessage:(YNTwoPeopleChat *)twoPeopleChat];
}

/**
 * @bried 一次保存N条历史消息
 *
 * @param twoPeopleChatArray 消息数组
 */
+ (BOOL)saveMuchTwoPeopleChatWithMessage:(NSArray *)twoPeopleChatArray{
    return [YNContactManager saveMuchTwoPeopleChatWithMessage:(NSArray *)twoPeopleChatArray];
}


/**
 用于消息漫游判断
 查询最近多少条信息
 
 @param UserNo 聊天对方账号
 @param sendTime 这个时间之前的多少条数据，nil：最近多少条数据
 @param numberCount 查询多少条数据
 @return 返回结果
 */
+(NSArray *)queryTwoPeopleChatTopInfoWithUserNo:(NSString *)UserNo mTime:(NSString *)mTime numberCount:(NSInteger)numberCount{
    return [YNContactManager queryTwoPeopleChatTopInfoWithUserNo:(NSString *)UserNo mTime:(NSString *)mTime numberCount:(NSInteger)numberCount];
}


/**
 用于消息漫游判断
 群、讨论组、项目组、会话查询最近多少条信息
 
 @param groupNo 群、讨论组、项目组id
 @param groupType 类型
 @param sendTime  这个时间之前的多少条数据，nil：最近多少条数据
 @param numberCount 查询多少条数据
 @return 返回结果
 */
+(NSArray *)queryManPeopleChatTopInfoWithGroupNo:(NSString *)groupNo GroupType:(int)groupType mTime:(NSString *)mTime numberCount:(NSInteger)numberCount{
    return [YNContactManager queryManPeopleChatTopInfoWithGroupNo:(NSString *)groupNo GroupType:(int)groupType mTime:(NSString *)mTime numberCount:(NSInteger)numberCount];
}

/**
 用于消息漫游判断
 广播通知查询最近多少条信息
 
 @param mTime 时间
 @param numberCount 查询多少条数据
 @return
 */
+(NSArray *)queryBroadcastChatTopInfomTime:(NSString *)mTime numberCount:(NSInteger)numberCount{
    return [YNContactManager queryBroadcastChatTopInfomTime:(NSString *)mTime numberCount:(NSInteger)numberCount];
}


/**
 * @brief 清空消息
 *
 * @param UserNo 要清空消息的用户的UserNo, 如果传进来为
 *               nil，清空所有历史消息
 */
+ (BOOL)deleteTwoPeopleChatWithUserNo:(NSString *)UserNo{
    return [YNContactManager deleteTwoPeopleChatWithUserNo:(NSString *)UserNo];
}

/**
 * @brief 查询两人会话历史消息
 *
 * @param UserNo 需要查询的用户的UserNo
 *        start  起始
 *        end    需要返回好多条信息(一般为20)
 */
+ (NSArray *)queryTwoPeopleChatHistoryMessageWithUserNo:(NSString *)UserNo startIndex:(NSInteger)start endIndex:(NSInteger)end{
    return [YNContactManager queryTwoPeopleChatHistoryMessageWithUserNo:(NSString *)UserNo startIndex:(NSInteger)start endIndex:(NSInteger)end];
}
+ (NSArray *)queryTwoPeopleChatHistoryMessageWithUserNo:(NSString *)UserNo startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess{
    return [YNContactManager queryTwoPeopleChatHistoryMessageWithUserNo:(NSString *)UserNo startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess];
}

/**
 查询两人会话未读第一条信息
 
 @param userNo 聊天信息id
 @param unReadCount 未读条数
 @return 返回数据
 */
+(NSArray *)queryUnReadFirstTwoPeopleChatMessageWithUserNo:(NSString *)userNo unReadCount:(NSInteger)unReadCount{
    return [YNContactManager queryUnReadFirstTwoPeopleChatMessageWithUserNo:(NSString *)userNo unReadCount:(NSInteger)unReadCount];
}

/**
 * @brief 根据关键字查询历史消息
 *
 * @param keyWords 用户输入的关键字
 **/
+ (NSArray *)queryTwoPeopleChatByKeywords:(NSString *)keyWords withUserNo:(NSString *)userNo{
    return [YNContactManager queryTwoPeopleChatByKeywords:(NSString *)keyWords withUserNo:(NSString *)userNo];
}

#pragma mark - ManyPeopleChat Method

/**
 * @brief 保存一条消息到多人历史消息表
 *
 * @param manyPeopleChat 需要保存的消息对象
 * @param groupType      判断是群消息还是讨论组消息
 **/
+ (BOOL)saverManyPeopleChatWithMesage:(YNManyPeopleChat *)manyPeopleChat groupType:(int)groupType{
    return [YNContactManager saverManyPeopleChatWithMesage:(YNManyPeopleChat *)manyPeopleChat groupType:(int)groupType];
}

/**
 * @brief 清空消息
 *
 * @param GroupNo   需要清空消息的群或讨论组的GroupNo,如果为NIL，清空所有消息
 *        groupType 判断是群消息还是讨论组消息
 **/
+ (BOOL)deleteManyPeopleChatWithGroupNo:(NSString *)GroupNo groupType:(int)groupType{
    return [YNContactManager deleteManyPeopleChatWithGroupNo:(NSString *)GroupNo groupType:(int)groupType];
}

/**
 * @brief 查询多人会话历史消息
 *
 * @param GroupNo   根据GroupNo查询消息
 *        groupType 判断是群消息还是讨论组消息
 *        start     起始
 *        end       需要返回好多条信息(一般为20)
 **/
+ (NSArray *)queryManyPeopleChatHistoryMessageWithGroupNo:(NSString *)GroupNo groupType:(int)groupType startIndex:(NSInteger)start endIndex:(NSInteger)end{
    return [YNContactManager queryManyPeopleChatHistoryMessageWithGroupNo:(NSString *)GroupNo groupType:(int)groupType startIndex:(NSInteger)start endIndex:(NSInteger)end];
}

+ (NSArray *)queryManyPeopleChatHistoryMessageWithGroupNo:(NSString *)GroupNo groupType:(int)groupType startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess{
    return [YNContactManager queryManyPeopleChatHistoryMessageWithGroupNo:(NSString *)GroupNo groupType:(int)groupType startIndex:(NSInteger)start endIndex:(NSInteger)end isSelectSendSuccessMessage:(BOOL)selectSuccess];
}

/**
 查询多人对话未读消息的第一条消息
 
 @param GroupNo 当前聊天所在的讨论组/项目组或群的ID
 @param groupType 类型
 @param unReadCount 未读消息条数
 @return 返回数据
 */
+ (NSArray *)queryUnReadFirstManyPeopleChatGroupNo:(NSString *)GroupNo groupType:(int)groupType unReadCount:(NSInteger)unReadCount{
    return [YNContactManager queryUnReadFirstManyPeopleChatGroupNo:(NSString *)GroupNo groupType:(int)groupType unReadCount:(NSInteger)unReadCount];
}

/**
 * @brief 根据关键字查询历史消息
 *
 * @param keyWords  用户输入的关键字
 *        groupType 判断是群消息还是讨论组消息
 **/
+ (NSArray *)queryManyPeopleChatByKeywords:(NSString *)keyWords groupNo:(NSString *)groupNo groupType:(int)groupType{
    return [YNContactManager queryManyPeopleChatByKeywords:(NSString *)keyWords groupNo:(NSString *)groupNo groupType:(int)groupType];
}

/**
 查询艾特@数据
 2018-11-16 JC添加
 @param groupType
 @param groupNo
 @return 返回结果
 */
+ (NSArray *)queryManyPeopleChatAitWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo{
    return [YNContactManager queryManyPeopleChatAitWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo];
}

/**
 根据groupNo查询该条信息
 2019-1-30 JC添加
 @param groupType
 @param groupNo
 @return 返回结果
 */
+ (NSArray *)queryManyPeopleChatWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo{
    return [YNContactManager queryManyPeopleChatWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo];
}

/**
 查询艾特@个人或艾特@所有人，离最新一条数据共有多少条数据
 2018-11-16 JC添加
 
 @param groupType
 @param groupNo
 @param aitContactMark 1：艾特@自己，2：艾特所有人
 @return 返回结果
 */
+ (NSInteger)queryManyPeopleChatAitCountWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo ynmanyPeopleChat:(YNManyPeopleChat*)ynmanyPeopleChat{
    return [YNContactManager queryManyPeopleChatAitCountWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo ynmanyPeopleChat:(YNManyPeopleChat*)ynmanyPeopleChat];
}

/**
 修改艾特@数据为默认0的数据
 2018-11-16 JC添加
 @param groupType
 @param groupNo
 */
+ (BOOL)updateManyPeopleChatAitWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo{
    return [YNContactManager updateManyPeopleChatAitWithGroupTyoe:(int)groupType groupNo:(NSString *)groupNo];
}


#pragma mark - 根据id获取名字
/**
 * @brief 根据id获取名字
 **/
+ (id)queryUserNameByUserID:(NSString *)userID chatType:(int)chatType{
    return [YNContactManager queryUserNameByUserID:(NSString *)userID chatType:(int)chatType];
}

#pragma mark - 保存同步下来的会话列表
/**
 * 将同步下来的会话列表保存到数据库
 * pram: array
 */
+ (BOOL)saveSessionListWithInfo:(NSArray *)infos{
    return [YNContactManager saveSessionListWithInfo:(NSArray *)infos];
}

/**
 解析和保存拉取到的漫游消息
 
 @param infos 消息数组
 @param isNewListPullData YES:消息列表拉取最新消息漫游
 @return 结果
 */
+ (BOOL)parsingAndSaveRoamMessageWithInfo:(NSArray *)infos isMessageListData:(BOOL)isMessageListData isNewListPullData:(BOOL)isNewListPullData{
    return [YNContactManager parsingAndSaveRoamMessageWithInfo:(NSArray *)infos isMessageListData:(BOOL)isMessageListData isNewListPullData:(BOOL)isNewListPullData];
}

/**
 *  @brief 根据parentOrgNo获取组织机构
 *
 *  @param parentOrgNo 父节点
 *
 *  @return 结果按照ordId降序排列
 */
+ (NSArray *)queryOrgTreeByParentOrgNo:(NSString *)parentOrgNo{
    return [YNContactManager queryOrgTreeByParentOrgNo:(NSString *)parentOrgNo];
}

/**
 根据userNo获取本条消息
 
 @param userNo
 @return
 */
+ (NSArray *)queryRecentChatWithUserNo:(NSString *)userNo{
    return [YNContactManager queryRecentChatWithUserNo:(NSString *)userNo];
}

/**
 根据packId获取本条消息
 
 @param packId
 @return
 */
+ (NSArray *)queryRecentChatWithPackId:(NSString *)packId{
    return [YNContactManager queryRecentChatWithPackId:packId];
}

/**
 查询会话人的消息记录里时间最早的时间
 黄旭 2017-12-5
 @param userNo 用户id
 @param db 数据库对象
 @return 时间字符串
 */
+ (NSString*)queryMinTimeByTwoPeopleChatWithUserNo:(NSString*)userNO{
    return [YNContactManager queryMinTimeByTwoPeopleChatWithUserNo:(NSString*)userNO];
}

//根据userNo查询本条信息
+ (NSArray*)queryTwoPeopleChatWithUserNo:(NSString*)userNO{
    return [YNContactManager queryTwoPeopleChatWithUserNo:(NSString*)userNO];
}

/**HX** 将userNo未读消息的未读数做加一或减一的更新 **/
+ (BOOL)updateUserUnreadCountByOneWithUserNo:(NSString*)UserNo isAddOne:(BOOL)addOne{
    return [YNContactManager updateUserUnreadCountByOneWithUserNo:(NSString*)UserNo isAddOne:(BOOL)addOne];
}
/**
 *  @brief  根据组织机构ID查询下面的所有user对象
 *
 *  @param orgNo   组织机构ID
 *
 *  noTeacher 是否不包含老师
 *
 *  @return YNUser数据数组
 */
+ (NSArray<YNUser*>*)queryAllUsersByOrgNos:(NSString *)orgNos isNoTeacher:(BOOL)noTeacher{
    return [YNContactManager queryAllUsersByOrgNos:(NSString *)orgNos isNoTeacher:(BOOL)noTeacher];
}

/**
 查询自己在群组里的角色（是否是管理员）
 
 @param groupType
 @return 结果集
 */
+ (NSArray<NSDictionary*> *)queryMyGroupRoleWithGroupId:(NSString*)groupId{
    return [YNContactManager queryMyGroupRoleWithGroupId:(NSString*)groupId];
}
/**
 删除最近会话列表里的所有系统和通知消息
 
 @return
 */
+ (BOOL)deleteRecentSystemAndInformChat{
    return [YNContactManager deleteRecentSystemAndInformChat];
}
#pragma mark - 更新会话消息
/**
 * 收到回执，更新数据库里面的消息
 * isChange是选择最近会话是否全更新
 */
+ (BOOL)updateChatWithMessage:(YNMessage *)message changRecentChat:(BOOL)isChange{
    return [YNMessageContactManager updateChatWithMessage:(YNMessage *)message changRecentChat:(BOOL)isChange];
}

//更新未读标识
+ (BOOL)updateUnReadMessageNumberWithIdentify:(NSString *)identify{
    return [YNMessageContactManager updateUnReadMessageNumberWithIdentify:(NSString *)identify];
}

//将广播消息标记为已读
+ (BOOL)updateIsReadToReadInDatabaseWithPacketid:(NSString *)packetid{
    return [YNMessageContactManager updateIsReadToReadInDatabaseWithPacketid:(NSString *)packetid];
}

//更新消息的高度
+ (BOOL)updateMessageHeightWithMessage:(YNMessage*)message{
    return [YNMessageContactManager updateMessageHeightWithMessage:(YNMessage*)message];
}

/**JC** 将isNewListPullData更新为YES，表示已经拉取过消息漫游 **/
+ (BOOL)updateRecentNewListPullDataUnreadCountByOneWithUserNo:(NSString *)userNo{
    return [YNMessageContactManager updateRecentNewListPullDataUnreadCountByOneWithUserNo:(NSString *)userNo];
}

//删除广播消息: if packetId = nil , delete ALL Broadcast.
+ (BOOL)deleteBroadcastMessageWithPacketId:(NSString *)packetId{
    return [YNMessageContactManager deleteBroadcastMessageWithPacketId:(NSString *)packetId];
}

//查询20条广播消息
+ (NSArray *)queryBroadcastWithStartIndex:(NSInteger)start
                                 endIndex:(NSInteger)end{
    return [YNMessageContactManager queryBroadcastWithStartIndex:(NSInteger)start
                                                        endIndex:(NSInteger)end];
}

#pragma mark - Broadcast Notification Mothed
//删除广播通知数据
+ (BOOL)deleteBroadcastNotificationWithObject:(id)object{
    return [YNMessageContactManager deleteBroadcastNotificationWithObject:(id)object];
}

//查询广播通知数据
+ (NSArray *)queryBroadNotificationWithType:(YNBroadcastNotificationType)broadcastNotificationType{
    return [YNMessageContactManager queryBroadNotificationWithType:(YNBroadcastNotificationType)broadcastNotificationType];
}

//查询未读广播通知数据
+ (NSArray *)queryUnReadBroadNotificationWithType:(YNBroadcastNotificationType)broadcastNotificationType{
    return [YNMessageContactManager queryUnReadBroadNotificationWithType:(YNBroadcastNotificationType)broadcastNotificationType];
}

//删除一部分广播通知数据
+ (BOOL)deleteBroadcastNotificationWithMessageObject:(id)object{
    return [YNMessageContactManager deleteBroadcastNotificationWithMessageObject:(id)object];
}

//pocketId为空则更新全部未读通知
+ (BOOL)updateUnReadBroadNotificationWithType:(YNBroadcastNotificationType)broadcastNotificationType pocketId:(NSString *)pocketId{
    return [YNMessageContactManager updateUnReadBroadNotificationWithType:(YNBroadcastNotificationType)broadcastNotificationType pocketId:(NSString *)pocketId];
}
#pragma end

/**
 *  在登录成功以后, 调用此方法
 *  1.更新两人会话, 将会话中发送中的状态转变成发送失败状态
 *  2.更新多人会话, 将会话中发送中的状态转变成发送失败状态
 *  3.更新最近会话, 将会话中发送中的状态转变成发送失败状态
 */
+ (BOOL)updateMessageSendingStateWhenLoggedIn{
    return [YNMessageContactManager updateMessageSendingStateWhenLoggedIn];
}

/**
 * 根据传来的消息删除对应的会话消息
 */
+ (BOOL)deleteAChatMessageFromMessageData:(YNMessage *)message{
    return [YNMessageContactManager deleteAChatMessageFromMessageData:(YNMessage *)message];
}

// 图片下载后更新
+ (BOOL)updateMessageImageWithMessage:(YNMessage*)message{
    return [YNMessageContactManager updateMessageImageWithMessage:(YNMessage*)message];
}

//更新文件消息状态
+ (BOOL)updateMessageFileStateWithMessage:(YNMessage*)message{
    return [YNMessageContactManager updateMessageFileStateWithMessage:(YNMessage*)message];
}

/**
 *  更新语音已读状态 2016.8.1jc添加
 *
 *  @param message message对象
 *
 *  @return 返回参数
 */
+(BOOL)updateMessageVoiceStateWithMessage:(YNMessage *)message{
    return [YNMessageContactManager updateMessageVoiceStateWithMessage:(YNMessage *)message];
}

/**
 修改两人会话撤回状态
 
 @param packId ID
 @return
 */
+(BOOL)updateTwoPeopleChatWithdrawWithPackId:(NSString *)packId{
    return [YNMessageContactManager updateTwoPeopleChatWithdrawWithPackId:(NSString *)packId];
}

/**
 JC修改群，讨论组，项目组撤回状态
 
 @param packId id
 @param groupType
 @return
 */
+(BOOL)updateManyPeopleMessageWithdrawWithPacketId:(NSString *)packId groupType:(int)groupType{
    return [YNMessageContactManager updateManyPeopleMessageWithdrawWithPacketId:(NSString *)packId groupType:(int)groupType];
}

/**
 修改撤回状态  2018.11.27jc添加
 
 @param chatId 对方id
 @param packId 消息id
 @return
 */
+(BOOL)updateRecentChatWithdrawWithChatId:(NSString *)chatId packId:(NSString *)packId{
    return [YNMessageContactManager updateRecentChatWithdrawWithChatId:(NSString *)chatId packId:(NSString *)packId];
}

/**HX** 获取会话列表本地存的指定userNo的未读数 **/
+ (int)queryUnReadWithUserNo:(NSString *)userNo{
    return [YNMessageContactManager queryUnReadWithUserNo:(NSString *)userNo];
}

///保存新的群组的提示设置
+ (BOOL)saveNewGroupRemind:(NSString *)groupId{
    return [YNRemindManager saveNewGroupRemind:groupId];
}
///imId兼容转化为平台userId
+(NSString*)getUseridFormImId:(NSString*)imId{
    return [YNIMTool getUseridFormImId:imId];
}

@end
